import time

from pages.loginPage import LoginPage
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


class MsgPage(LoginPage):
    my_message=(By.XPATH,'/html/body/div[5]/div[1]/div/div/div/div/a[6]')#点击我的留言按钮
    type=(By.XPATH,'/html/body/div[5]/div[2]/div/div/div/form/table/tbody/tr[1]/td[2]')
    zhuti=(By.XPATH,"/html/body/div[5]/div[2]/div/div/div/form/table/tbody/tr[2]/td[2]/input")#点击留言主题文本框
    liuyan=(By.XPATH,'/html/body/div[5]/div[2]/div/div/div/form/table/tbody/tr[3]/td[2]/textarea')#点击留言文本框
    wenjian=(By.XPATH,'/html/body/div[5]/div[2]/div/div/div/form/table/tbody/tr[4]/td[2]/input')#点击上传文件
    tijiao=(By.XPATH,'/html/body/div[5]/div[2]/div/div/div/form/table/tbody/tr[5]/td[2]/input[2]')#点击提交按钮
    def findEle(self,locator):
        try:
            print("开始查找元素")
            ele=WebDriverWait(self.driver,10,1).until(lambda d:d.find_element(*locator))
        except Exception as e:
            print(f"查找元素发生异常{locator},异常信息是{e}")
        else:
            print(f"元素找到了{locator}")
            return ele
        finally:
            print(f"查找完成{locator}")
    def message(self,a=3,sub="退货",con="商品质量不好",b=3):
        path1 = self.findEle(self.my_message)
        self.inputText2(path1)
        time.sleep(2)
        # 选择类型
        type1 = self.findEle(type)
        self.inputText2(type1)
        time.sleep(1)
        #输入主题
        sub1 = self.findEle(self.zhuti)
        self.inputText(sub1, sub)
        time.sleep(1)
        # 输入内容
        con1 = self.findEle(self.liuyan)
        self.inputText(con1, con)
        time.sleep(1)
        #上传文件
        file1 = self.findEle(self.wenjian)
        self.inputText2(file1)
        time.sleep(1)
        #点击提交
        but1=self.findEle(self.tijiao)
        self.inputText2(but1)
        time.sleep(3)

        #删除
        # def delete(self):
        delete = (By.CSS_SELECTOR, 'a[title="删除"]')
        # delete=(By.XPATH,f'/html/body/div[5]/div[2]/div/div/div/div[[{b}]]/a')
        dele1 = self.findEle(delete)
        self.inputText2(dele1)
        time.sleep(1)
        alert: Alert = self.driver.switch_to.alert
        self.driver.switch_to.alert.accept()
        time.sleep(1)

if __name__=='__main__':
    ly=MsgPage()
    ly.login('hjy', "1234567A")
    ly.message(0,'留言',"推荐购买")
